/* 
 * Copyright 2011 kOSMik.
 * This file is part of kOSMik.
 * 
 * kOSMik is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * kOSMik is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with kOSMik.  If not, see <http://www.gnu.org/licenses/>.
 *  
 * kOSMik:
 * Open Street Map - Editor.
 * http://code.google.com/p/swp-dv-ws2010-osm-2/
 */

package de.fuberlin.kosmik.files;

import java.io.File;
import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Log;
import android.widget.Toast;
import de.fuberlin.kosmik.R;
import de.fuberlin.kosmik.data.DataBaseConnector;
import de.fuberlin.kosmik.data.DataBaseConnectorImpl;
import de.fuberlin.kosmik.log.Constants;

/**
 * Provides basis methods to capture video and image files within an activity.
 * 
 * @author Michael Schmidt
 * 
 */
public class MediaController {
	/**
	 * Constant defining audio media type. Used in the database and as base name
	 * of file prefix.
	 */
	public static final String MEDIA_TYPE_AUDIO = "audio";

	/**
	 * Constant defining video media type. Used in the database and as base name
	 * of file prefix.
	 */
	public static final String MEDIA_TYPE_VIDEO = "video";

	/**
	 * Constant defining image type. Used in the database and as base name of
	 * file prefix.
	 */
	public static final String MEDIA_TYPE_IMAGE = "image";

	/**
	 * Constant defining text type. Used in the database.
	 */
	public static final String MEDIA_TYPE_TEXT = "text";

	/**
	 * Request code of intent for recording video.
	 */
	public static final int INTENT_RECORD_VIDEO = 34;

	/**
	 * Request code of intent for taking pictures.
	 */
	public static final int INTENT_TAKE_PICTURE = 35;

	private long point;
	private FileManager fileManager;
	private File file;

	/**
	 * Activity that uses the MediaController class.
	 */
	Activity activity;

	/**
	 * Constructor.
	 * 
	 * @param activity owner activity
	 * @param trackId id of the current track
	 */
	public MediaController(Activity activity, long trackId) {
		try {
			this.fileManager = new FileManagerImpl(trackId);
		} catch (IOException e) {
			Log.d(Constants.TAG, "illegal media state");
			Toast.makeText(activity, activity.getString(R.string.err_media_not_ready),
					Toast.LENGTH_LONG).show();
		}
		this.activity = activity;
	}

	/**
	 * Method is sending intent MediaStore.ACTION_IMAGE_CAPTURE to call build-in
	 * system activity to capture image.
	 * 
	 * @param pointId id of the point, associated with the image
	 */
	public void recordImage(long pointId) {
		file = new File(fileManager.getAbsoluteBasePath(),
				fileManager.getFilename(MEDIA_TYPE_IMAGE) + ".jpg");
		point = pointId;

		Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
		activity.startActivityForResult(intent, INTENT_TAKE_PICTURE);
	}

	/**
	 * If result code is indicating RESULT_OK, this method puts an entry into
	 * database.
	 * 
	 * @param resultCode result code of
	 */
	public void handleImageResult(int resultCode) {
		if (resultCode == Activity.RESULT_OK) {
			(new Thread(new SavingHelper(MEDIA_TYPE_IMAGE, file.toString(), point))).start();
		}
	}

	private class SavingHelper implements Runnable {
		private long pointId;
		private String fileName;
		private String type;

		public SavingHelper(String type, String file, long point) {
			this.pointId = point;
			this.fileName = file;
			this.type = type;
		}

		@Override
		public void run() {
			Log.d(Constants.TAG, "Saving file: " + fileName + " at point[id: " + pointId + "]");
			DataBaseConnector db = new DataBaseConnectorImpl(activity);
			db.addMedia(type, fileName, pointId);
			db.close();
		}
	}

	/**
	 * The file, which is currently recorded.
	 * 
	 * @return file
	 */
	public File getCurrentFile() {
		return file;
	}

	/**
	 * Set the file, which is currently recorded.
	 * 
	 * @param fileName file name
	 */
	public void setCurrentFile(String fileName) {
		file = new File(fileName);
	}
}
